package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long id) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category -> {
                Long count = baseMapper.selectCount(
                        new LambdaQueryWrapper<Category>()
                                .eq(Category::getParentId, category.getId())
                );
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询所有一级分类
        List<Category> allCategoryList = baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        //转换为vo
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);
        //转化为Vo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        //转化为树形结构
        return buildTree(categoryVoList);
    }

    /**
     * @description:使用递归方法建分类树
     * @author: coilk
     * @date: 2025/3/4 9:13
     * @param: categoryVoList
     * @return: java.util.List<com.spzx.product.vo.CategoryVo>
     **/
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否为根
            if (categoryVo.getParentId().longValue() == 0) {
                //查询当前节点的子节点,添加到树形结构中
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                trees.add(categoryVo);
            }
        }
        return trees;
    }

    /**
     * @description:在categoryVoList中找到categoryVo的子节点，并组装到categoryVo中
     * @author: coilk
     * @date: 2025/3/4 9:14
     * @param: categoryVo
     * @param: categoryVoList
     * @return: com.spzx.product.vo.CategoryVo
     **/
    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            //判断是否是categoryVO的子节点
            if (node.getParentId().longValue() == categoryVo.getId().longValue()) {
                //在categoryVoList中找到categoryVo的子节点
                CategoryVo children = findAndAddChildren(node, categoryVoList);
                //组装到categoryVo中
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }

}
